import 'package:flutter/services.dart';
import 'package:flutter/material.dart';


enum InputCharFormatterType {
  InputCharFormatterNone,    //无限制
  InputCharFormatterDigital, //数字
  InputCharFormatterDecimal  //小数和整数
}

/// 单行输入
class SingleInputItemWidget extends StatefulWidget {
  //标题
  final String title;
  //占位符
  final String placeholder;
  //最多输入文字
  final int maxLength;
  //文本控制器
  final TextEditingController controller;
  //焦点
  final FocusNode focusNode;
  //占位符风格
  final TextStyle placeholderStyle;
  //文字风格
  final TextStyle textStyle;
  //文字背景颜色
  final Color textBgColor;
  //文字背景颜色
  final Color bgColor;
  //是否可编辑
  final bool readOnly;
  //按下return（完成键）键收起键盘
  final bool returnKeyDownKeyBoard;
  //是否底部有分割线
  final bool isLine;

  final InputCharFormatterType inputCharFormatterType;
  final bool enableInteractiveSelection;

  SingleInputItemWidget({
    @required this.title,
    this.placeholder = "",
    this.maxLength = 100,
    @required this.controller,
    this.focusNode,
    this.textStyle,
    this.placeholderStyle,
    this.textBgColor,
    this.bgColor,
    this.readOnly = false,
    this.returnKeyDownKeyBoard = true,
    this.isLine = true,
    this.inputCharFormatterType = InputCharFormatterType.InputCharFormatterNone,
    this.enableInteractiveSelection = false,
  });
  @override
  WidgetSingleInputState createState() => WidgetSingleInputState();
}

class WidgetSingleInputState extends State<SingleInputItemWidget> {

  int getMaxLength(){
    //设置了return键放下键盘和最大长度之后，应该设置最大长度再加1
    if(this.widget.returnKeyDownKeyBoard == true && this.widget.maxLength != null) {
      int maxLength = this.widget.maxLength + 1;
      return maxLength;
    } else {
      return this.widget.maxLength;
    }
  }

  List<TextInputFormatter > getInputFormatter(InputCharFormatterType inputCharFormatterType) {
    if(inputCharFormatterType == InputCharFormatterType.InputCharFormatterNone) return null;
    List<TextInputFormatter> formatters =  List<TextInputFormatter>();
    if(inputCharFormatterType == InputCharFormatterType.InputCharFormatterDigital) {
       formatters.add(WhitelistingTextInputFormatter.digitsOnly);
    } else if(inputCharFormatterType == InputCharFormatterType.InputCharFormatterDecimal) {
      formatters.add(WhitelistingTextInputFormatter(RegExp("[0-9.]")));
    }
    return formatters;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: this.widget.textBgColor ?? Colors.white,
      child: Column(
        children: <Widget>[
          Container(
            height: 48,
            padding: EdgeInsets.symmetric(horizontal: 16),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Container(
                  width: 100,
                  child: Text(widget.title,style: TextStyle(color: Color(0xff666666),fontSize: 14),),
                ),
                SizedBox(width: 8,),
                Expanded(
                  child: Container(
                    child: TextField(
                      autocorrect: false,
                      textAlign: TextAlign.right,
                      readOnly: this.widget.readOnly,
                      enableInteractiveSelection: widget.enableInteractiveSelection,
                      focusNode: this.widget.focusNode,
                      inputFormatters: getInputFormatter(widget.inputCharFormatterType),
                      controller: this.widget.controller,
                      onChanged: (value) {
                        debugPrint("$value");
                        if (value.contains("\n") && this.widget.returnKeyDownKeyBoard == true) {
                          this.widget.controller.text = this
                              .widget
                              .controller
                              .text
                              .replaceAll("\n", "");
                          this.widget.focusNode.unfocus();
                        } else if(value.length > this.widget.maxLength && this.widget.returnKeyDownKeyBoard == true) {
                          this.widget.controller.text = this
                              .widget
                              .controller
                              .text
                              .replaceRange(this.widget.maxLength, this.widget.maxLength + 1, "");
                          this.widget.focusNode.unfocus();
                        } else if(value.length == this.widget.maxLength) {
                          this.widget.focusNode.unfocus();
                        }

                        setState(() {});
                      },
                      maxLength: this.getMaxLength(),
                      maxLines: 1,
                      style: this.widget.textStyle ??
                          TextStyle(fontSize: 13, color: Color(0xFF999999),),
                      enabled: true,
                      autofocus: false,
                      decoration: InputDecoration(
                          contentPadding: const EdgeInsets.all(2),
                          border: InputBorder.none,
                          hintText: this.widget.placeholder,
                          hintStyle: this.widget.placeholderStyle ??
                              TextStyle(fontSize: 13, color: Color(0xFF999999)),
                          counterText: ""),
                    ),

                  ),
                ),
              ],
            ),
          ),
          Container(
            padding: EdgeInsets.only(left: 16),
            child: Offstage(
              offstage: !widget.isLine,
              child: Container(
                height: 0.5,
                color: Color(0xffebebeb),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
